const Service = require('uni-cloud-router').Service
// 继承SerVice 处理逻辑
module.exports = class DisposeDataController extends Service {
	async allotDrillInProject({
		projectID,
		drillID
	}) {
		let allotDrillRes = await this.db.collection('project_store').where({
			_id: projectID
		}).update({
			drill_id: drillID
		})

		if (allotDrillRes.affectedDocs !== 0) {
			return {
				status: 0,
				msg: "分配成功"
			}
		} else {
			return {
				status: 0,
				msg: "分配失败"
			}
		}

		return {
			status: -1,
			msg: "稍后重试"
		}
	}

	async allotWorkerInProject({
		projectID,
		worker_ids,
		salaryWay
	}) {
		let allotWorkerRes = await this.db.collection('project_store').where({
			_id: projectID
		}).update({
			worker_ids
		})

		if (allotWorkerRes.affectedDocs !== 0) {
			return {
				status: 0,
				msg: "分配成功"
			}
		} else {
			return {
				status: 0,
				msg: "分配失败"
			}
		}

		return {
			status: -1,
			msg: "稍后重试"
		}
	}

	async checkDrillID({
		id
	}) {
		let drillDataInDB = await this.db.collection('drill_store').where({
			drill_id: id
		}).get();

		if (drillDataInDB.affectedDocs !== 0 || drillDataInDB.data.length !== 0) {
			return {
				status: 1,
				msg: "编号重复"
			}
		} else {
			return {
				status: 0,
				msg: "编号合格"
			}
		}

		return {
			status: -1,
			msg: "稍后重试"
		}
	}

	async checkPartsID({
		id
	}) {
		let partsDataInDB = await this.db.collection('parts_store').where({
			parts_id: id
		}).get();

		if (partsDataInDB.affectedDocs !== 0 || partsDataInDB.data.length !== 0) {
			return {
				status: 1,
				msg: "编号重复"
			}
		} else {
			return {
				status: 0,
				msg: "编号合格"
			}
		}

		return {
			status: -1,
			msg: "稍后重试"
		}
	}

	async checkProjectID({
		id
	}) {
		let projectDataInDB = await this.db.collection('project_store').where({
			project_id: id
		}).get();

		if (projectDataInDB.affectedDocs !== 0 || projectDataInDB.data.length !== 0) {
			return {
				status: 1,
				msg: "编号重复"
			}
		} else {
			return {
				status: 0,
				msg: "编号合格"
			}
		}

		return {
			status: -1,
			msg: "稍后重试"
		}
	}

	async checkWorkID({id}) {

		let workerDataInDB = await this.db.collection('userdata').where({
			wkID: id
		}).get();

		if (workerDataInDB.affectedDocs !== 0 || workerDataInDB.data.length !== 0) {
			return {
				status: 1,
				msg: "编号重复"
			}
		} else {
			return {
				status: 0,
				msg: "编号合格"
			}
		}

		return {
			status: -1,
			msg: "稍后重试"
		}
	}

	async deleteDrillInProject({
		drill_id
	}) {
		let deleteDrillIDInProject = await this.db.collection('project_store').where({
			drill_id
		}).update({
			drill_id: ""
		})

		if (deleteDrillIDInProject.affectedDocs !== 0) {
			return {
				status: 0,
				msg: "删除成功"
			}
		} else {
			return {
				status: 1,
				msg: "删除失败"
			}
		}

		return {
			status: -1,
			msg: "稍后重试！"
		}
	}

	async getDrillInfo() {
		let drillDataInfo = await this.db.collection('drill_store').get();

		if (drillDataInfo.affectedDocs !== 0 || drillDataInfo.data.length !== 0) {
			return {
				status: 0,
				msg: "成功",
				data: drillDataInfo.data
			}
		} else {
			return {
				status: 1,
				msg: "没有数据"
			}
		}

		return {
			status: -1,
			msg: "稍后重试"
		}
	}
	
	async getDrillInfoByPmCompany(pm_company) {
		let drillDataInfo = await this.db.collection('drill_store').where({
			pm_company
		}).get();
	
		if (drillDataInfo.affectedDocs !== 0 || drillDataInfo.data.length !== 0) {
			return {
				status: 0,
				msg: "成功",
				data: drillDataInfo.data
			}
		} else {
			return {
				status: 1,
				msg: "没有数据"
			}
		}
	
		return {
			status: -1,
			msg: "稍后重试"
		}
	}
	
	
	async setDrillInfo({
		drill_id,
		drill_price,
		drill_buy_date,
		drill_status,
		maintain_record,
		pm_company
	}) {

		let drillInfoInDB = await this.db.collection('drill_store').where({
			drill_id
		}).get();

		if (drillInfoInDB.affectedDocs !== 0 || drillInfoInDB.data.length !== 0) {
			let editDrillInfo = await this.db.collection('drill_store').where({
				drill_id
			}).update({
				drill_id,
				drill_price,
				drill_buy_date,
				maintain_record
			})

			if (editDrillInfo.affectedDocs !== 0) {
				return {
					status: 0,
					mag: "保存成功"
				}
			}
		} else {
			let addDrillInfo = await this.db.collection('drill_store').add({
				drill_id,
				drill_price,
				drill_buy_date,
				maintain_record,
				drill_status,
				pm_company
			})

			if (addDrillInfo.affectedDocs !== 0) {
				return {
					status: 0,
					mag: "保存成功"
				}
			}
		}

		return {
			status: -1,
			msg: "稍后重试"
		}
	}

	async getOneDrillInfo({
		_id
	}) {
		let drillDataInDB = await this.db.collection('drill_store').doc(_id).get();

		if (drillDataInDB.affectedDocs !== 0 || drillDataInDB.data.length !== 0) {
			return {
				status: 0,
				msg: "成功",
				data: drillDataInDB.data
			}
		} else {
			return {
				status: 1,
				msg: "没有数据"
			}
		}

		return {
			status: -1,
			msg: "稍后重试"
		}
	}

	async setPartsInfo({
		parts_type,
		buy_date,
		buy_address,
		buy_price,
		parts_id,
		parts_receipt
	}) {
		let partsDataInDB = await this.db.collection('parts_store').where({
			parts_id
		}).get();

		if (partsDataInDB.affectedDocs !== 0 || partsDataInDB.data.length !== 0) {
			//更新
			let updatePartsRes = await this.db.collection('parts_store').where({
				parts_id
			}).update({
				parts_type,
				buy_date,
				buy_address,
				buy_price,
				parts_id,
				parts_receipt
			})

			if (updatePartsRes.affectedDocs !== 0) {
				return {
					status: 0,
					msg: "保存成功"
				}
			}
		} else {
			//追加
			let addPartsRes = await this.db.collection('parts_store').add({
				parts_type,
				buy_date,
				buy_address,
				buy_price,
				parts_id,
				parts_receipt
			})

			if (addPartsRes.affectedDocs !== 0) {
				return {
					status: 0,
					msg: "保存成功"
				}
			}
		}

		return {
			status: -1,
			msg: "稍后重试"
		}
	}

	async getPartsInID({
		_id
	}) {
		let partsDataRes = await this.db.collection('parts_store').doc(_id).get();

		if (partsDataRes.affectedDocs !== 0 || partsDataRes.data.length !== 0) {
			return {
				status: 0,
				msg: "成功",
				data: partsDataRes.data
			}
		} else {
			return {
				status: 1,
				msg: "没有数据"
			}
		}

		return {
			status: -1,
			msg: "稍后重试"
		}
	}
	
	async getPartsInfo(){
		let partsDataInfo = await this.db.collection('parts_store').get();
		
		if(partsDataInfo.affectedDocs !== 0 || partsDataInfo.data.length !== 0){
			return {
				status:0,
				msg:"成功",
				data:partsDataInfo.data
			}
		} else {
			return {
				status:1,
				msg:"没有数据"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
}
